﻿Imports Controle
Public Class frmConfiguracoes

#Region "Variáveis"
    Private objNcm As New ctrNcm

    Private datNCM As New DataTable

#End Region

#Region "Enumerações"

    Private Enum eColsGridNCM
        NCM = 0
        IPI = 1
    End Enum

#End Region

    Private Sub configurarGrid()

        With Me.dgvNcm
            .DataSource = Nothing
            .AutoGenerateColumns = False
            .RowHeadersVisible = False
            .AllowUserToAddRows = False
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False
            .Columns.Clear()

            Dim colNcm As New DataGridViewTextBoxColumn()
            With colNcm
                .HeaderText = "NCM"
                .DataPropertyName = "ncm"
                .Width = 250
                .ReadOnly = True
                .Visible = True
                .ValueType = System.Type.GetType("System.string")
                .SortMode = DataGridViewColumnSortMode.NotSortable
            End With
            .Columns.Add(colNcm)

            Dim colIPI As New DataGridViewTextBoxColumn()
            With colIPI
                .HeaderText = "IPI"
                .DataPropertyName = "ipi"
                .Width = 55
                .ReadOnly = False
                .Visible = True
                .ValueType = System.Type.GetType("System.string")
                .SortMode = DataGridViewColumnSortMode.NotSortable
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            End With
            .Columns.Add(colIPI)

        End With
    End Sub

    Private Sub carregarNCM()
        Try
            datNCM = Me.objNcm.selecionarNCM("")
        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Critical, Application.ProductName)

        End Try


    End Sub

    Private Sub preencherGridNCM()
        Dim i As Integer = 0
        Try

            With dgvNcm
                .RowCount = datNCM.Rows.Count
                For Each drLinha As DataRow In datNCM.Rows
                    .Item(eColsGridNCM.NCM, i).Value = Persistencia.Conversao.nuloParaVazio(drLinha.Item("ncm"))
                    .Item(eColsGridNCM.IPI, i).Value = Format(Convert.ToDouble(Persistencia.Conversao.nuloParaZero(drLinha.Item("ipi"))), "#,##0.00")

                    i += 1
                Next
                .Sort(dgvNcm.Columns(eColsGridNCM.NCM))
            End With

        Catch ex As Exception

        End Try

    End Sub

    Private Sub frmConfiguracoes_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Me.configurarGrid()
        Me.carregarNCM()
        Me.preencherGridNCM()

    End Sub

    Private Sub dgvNcm_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvNcm.CellEndEdit

        Try
            If e.RowIndex >= 0 And e.ColumnIndex = eColsGridNCM.IPI Then
                dgvNcm.Item(eColsGridNCM.IPI, e.RowIndex).Value = Format(Convert.ToDouble(dgvNcm.Item(eColsGridNCM.IPI, e.RowIndex).Value), "#,##0.00")
            End If

        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Critical, Application.ProductName)

        End Try

    End Sub

    Private Sub cmdSair_Click(sender As System.Object, e As System.EventArgs) Handles cmdSair.Click

        Me.Close()

    End Sub

    Private Sub cmdOK_Click(sender As System.Object, e As System.EventArgs) Handles cmdOK.Click
        Dim datNCM As New DataTable

        Try
            datNCM = Persistencia.Conversao.converteGridParaDatatable(dgvNcm)
            If Me.objNcm.salvarNCM(datNCM) Then
                MessageBox.Show("Informações gravadas com sucesso!", Me.Text, MessageBoxButtons.OK)
            End If

        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Critical, Application.ProductName)


        End Try
    End Sub

    Private Sub btnAtualizar_Click(sender As System.Object, e As System.EventArgs) Handles btnAtualizar.Click
        Dim datNCMAtual As New DataTable

        Try
            Cursor = Cursors.WaitCursor

            datNCMAtual = Me.objNcm.atualizarNCMs
            If Not datNCMAtual Is Nothing Then
                datNCM.Load(datNCMAtual.CreateDataReader, LoadOption.PreserveChanges)
            End If

            Me.preencherGridNCM()

            Cursor = Cursors.Default

        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Critical, Application.ProductName)

        End Try
    End Sub

End Class